//
//  PartyFeeController.m
//  UESTCPioneer
//
//  Created by Apple on 14-7-1.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "PartyFeeController.h"
#import "PartyFeeTableViewCell.h"
//当前版本
#define FSystenVersion            ([[[UIDevice currentDevice] systemVersion] floatValue])
@interface PartyFeeController ()
{
    NSArray * _array;
}

@end

@implementation PartyFeeController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 320)];
    [self.view addSubview:container];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[PartyFeeTableViewCell class] forCellReuseIdentifier:@"PartyFee"];
    self.tableView.allowsSelection = NO;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 60, 30)];
//    UILabel * studentNo = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 60, 30)];
//    UILabel * branch = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, 60, 30)];
//    UIButton * sepBtn = [[UIButton alloc] initWithFrame:CGRectMake(2, 180, self.view.frame.size.width-4, 2)];
//    UILabel * feeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 100, 80)];
//    
//    [self.view addSubview:nameLabel];
//    [self.view addSubview:studentNo];
//    [self.view addSubview:branch];
//    [self.view addSubview:sepBtn];
//    [self.view addSubview:feeLabel];
        _array = @[@"姓名",@"学号",@"所属支部",@"应缴金额",@"缴费金额"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ScreenTouch)];
    [self.tableView addGestureRecognizer:tapGesture];
    NSLog(@"self.view.frame.origin.y = %f",self.view.frame.origin.y);
    
}

/**
 *	@brief	键盘出现
 *
 *	@param 	aNotification 	参数
 */
- (void)keyboardWillShow:(NSNotification *)aNotification

{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.view.frame = CGRectMake(0.f, -40.f, self.view.frame.size.width, self.view.frame.size.height);
        
    }completion:nil] ;
    
}

/**
 *	@brief	键盘消失
 *
 *	@param 	aNotification 	参数
 */
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    NSLog(@"keyboardWillHide");
    [UIView animateWithDuration:0.25 animations:^{
        NSLog(@"keyboardWillHide");
        self.view.frame = CGRectMake(0.f, 63.f, self.view.frame.size.width, self.view.frame.size.height);
        
    }completion:nil];
    
}

-(void)ScreenTouch{
//    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillHideNotification object:nil];
    [[self.view viewWithTag:1000] resignFirstResponder];

}

-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * identifier = @"PartyFee";
    PartyFeeTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[PartyFeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.nameLabel.text = _array[indexPath.row];
            cell.contentLabel.text = [constant getName];
        }else if (indexPath.row == 1){
            cell.nameLabel.text = _array[indexPath.row];;
//            cell.contentLabel.text = [constant get];
        
        }else if (indexPath.row == 2){
            cell.nameLabel.text = _array[indexPath.row];;
            cell.contentLabel.text = [constant getBranch] ;
        }else if (indexPath.row == 3){
            CGRect frame  = cell.frame;
            cell.frame = CGRectMake(frame.origin.x, frame.origin.y, self.view.frame.size.width, 70);
            
            cell.nameLabel.frame = CGRectMake(10, (cell.frame.size.height - 40)/2, 80, 40);
            cell.nameLabel.text = _array[indexPath.row];
            cell.contentLabel.frame = CGRectMake(100, (cell.frame.size.height - 40)/2, cell.frame.size.width - 100 - 50,40);
            cell.contentLabel.text = @"100";
            cell.nameLabel.font = [UIFont boldSystemFontOfSize:20];
            cell.contentLabel.font = [UIFont boldSystemFontOfSize:36];
            cell.contentLabel.textAlignment = NSTextAlignmentRight;
            UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width - 45, (cell.frame.size.height - 40)/2+5, 35,40)];
            label.text = @"元";
            label.font = [UIFont boldSystemFontOfSize:20];
            [cell.contentView addSubview:label];
            
            UIButton * button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 1)];
            [button1 setImage:[UIImage imageNamed:@"sepline.png"] forState:UIControlStateNormal];
            
            UIButton * button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 1, cell.frame.size.width, 1)];
            [button2 setImage:[UIImage imageNamed:@"sepline.png"] forState:UIControlStateNormal];
            
            [cell.contentView addSubview:button1];
            [cell.contentView addSubview:button2];
            NSLog(@"应缴金额cell.contentView.frame.size.height = %f",cell.frame.size.height);
        }
    }else if(indexPath.section == 1){
        CGRect frame  = cell.frame;
        cell.frame = CGRectMake(frame.origin.x, frame.origin.y, self.view.frame.size.width, 60);
        cell.nameLabel.frame = CGRectMake(10, (cell.frame.size.height - 40)/2, 80, 40);
        cell.nameLabel.text = _array[indexPath.row + 4];;
        
        UIButton * button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 1)];
        [button1 setImage:[UIImage imageNamed:@"sepline.png"] forState:UIControlStateNormal];
        
        UIButton * button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 1, cell.frame.size.width, 1)];
        [button2 setImage:[UIImage imageNamed:@"sepline.png"] forState:UIControlStateNormal];
        cell.nameLabel.font = [UIFont boldSystemFontOfSize:20];
        [cell.contentView addSubview:button1];
        [cell.contentView addSubview:button2];
        
        cell.contentTextField.frame = CGRectMake(100, (cell.frame.size.height - 40)/2, cell.frame.size.width - 100 -10, 40);
        cell.contentTextField.enabled = YES;
        cell.contentTextField.placeholder = @"请输入缴费金额";
        cell.contentTextField.tag = 1000;

       
    }else{
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = cell.frame;
        [button addTarget:self action:@selector(payPartyFee:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
        [button setTitle:@"立即缴费" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:18.f];
        [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
        
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"heightForRowAtIndexPath");
    
    if (indexPath.section == 1) {
        return 60;
    }else if(indexPath.section == 0){
        if (indexPath.row == 3) {
            return 70;
        }
    }
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

-(void)payPartyFee:(UIButton *)btn{
    [Alert showAlert:@"支付党费"];
}

#pragma mark - UITextFieldDelegate Method

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"textFieldDidBeginEditing");
        [UIView animateWithDuration:0.25 animations:^{
            self.view.frame = CGRectMake(0.f, -35.f, self.view.frame.size.width, self.view.frame.size.height);
        }completion:nil] ;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"textFieldDidEndEditing");
        [UIView animateWithDuration:0.25 animations:^{
                self.view.frame = CGRectMake(0.f, (FSystenVersion >= 7.0)?0.f:20.f, self.view.frame.size.width, self.view.frame.size.height);
                
            }completion:nil];
 
}
@end
